package com.uiotsoft.small.host.entity;

import com.uiotsoft.small.config.APPConfig;
import com.uiotsoft.small.config.HostConfig;
import com.uiotsoft.small.mqtt.MqttClientOld;
import com.uiotsoft.util.encrypt.MD5Utils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Data
@Slf4j
public class HostClient {
    private String sn;
    private String appSecret;
    private String appKey;
    private String username;
    private String password;
    private String clientId;
    private String mqttUrl;
    private String subTopic;
    private MqttClientOld myMqttClient;
    private String env;

    public HostClient(String env, String sn, String appKey, String appSecret) {
        this.env = env;
        this.sn = sn;
        this.appKey = appKey;
        this.appSecret = appSecret;
        clientId = "host-" + sn;
        Long time = System.currentTimeMillis() / 1000;
        username = appKey + "|" + sn + "|host|" + time;
        password = MD5Utils.MD5Encoding(username + appSecret, "");
        subTopic = "host/+/+/" + sn + "/+";

        if (env.toLowerCase().startsWith("chk")) {
            if (env.toLowerCase().endsWith("local")) {
                mqttUrl = HostConfig.Local_MqttUrl;
            } else {
                mqttUrl = HostConfig.CHK_MqttUrl;
            }
            myMqttClient = new MqttClientOld(mqttUrl, clientId);
            myMqttClient.setAppKey(APPConfig.CHK_appKey);
            myMqttClient.setAppSecret(APPConfig.CHK_appSecret);
        } else if (env.toLowerCase().startsWith("release")) {
            if (env.toLowerCase().endsWith("local")) {
                mqttUrl = HostConfig.Local_MqttUrl;
            } else {
                mqttUrl = HostConfig.Release_MqttUrl;
            }
            myMqttClient = new MqttClientOld(mqttUrl, clientId);
            myMqttClient.setAppKey(APPConfig.Release_appKey);
            myMqttClient.setAppSecret(APPConfig.Release_appSecret);
        }
    }
}
